  window.onload = function() {
      layui.use(['element', 'form', 'laydate', 'jquery', 'laypage'], function() {
          var element = layui.element,
              form = layui.form,
              laydate = layui.laydate,
              $ = layui.jquery,
              laypage = layui.laypage;


          laydate.render({
              elem: '#bg_time'
          });
          laydate.render({
              elem: '#ed_time'
          });

          $('.at_name').on('keyup', function() {
              var num = $(this).val().length;
              $('.changeNum').text(num)
          })
          $('.at_name').keyup();

          
          if(!$('#Th_tbody tr td').eq(0).find('input').val()){
            $('#Th_tbody tr td').eq(0).hide()
            $('.attr_modification .sku').hide()
          }

          $('.close_select_sku').click(function(){
            $('.attr_modification').show();
            $('.attr_select').hide();
          })

          $('.select_sku').click(function(){
            $('.attr_modification').hide();
            $('.attr_select').show();


            var id = $(".hide_id").val();
            var aid = $(".ac_id").val();
            var skuTbody = $("#sku_tbody");
            skuTbody.empty();
            $.post("/active/getsku", { id: id, aid:aid },function success(data) {
              if (data.code == 200) {
                dIv = '<thead><tr>';
                dIv += '<th width="40"><input type="checkbox" class="checkbox total" status="off"></th>';
                $(data.data.goods_attr.thead).each(function(i, item) {
                  dIv += '<th>' + item + '</th>'
                })
                dIv += '<th>拼团价</th>';
                // dIv += '<th>单购价</th>';
                dIv += '<th>原价</th>';
                dIv += '<th>库存</th>';
                dIv += '</tr></thead>';
                dIv += '<tbody id="t_b">';
                // " one_price="' + item.one_price + '
                $(data.data.goods_attr.tbody).each(function($key, item) {
                  dIv += '<tr><td><input type="checkbox" class="inputc child_ck" attr_value="' + item.attr_value + '"  original_price="' + item.original_price + '" discount_price="' + item.discount_price + '" cdn="'+ data.data.cdn +'" attr_img="' + item.images + '"   inventory="' + item.inventory + '" goods_attr_id="'+ item.attr_id +'"  attribute_value_id="' + item.attribute_value_id + '" id="' + item.id + '"></td>'
                  $(item.val).each(function(j, itemm) {
                    dIv += '<td>' + itemm.value + '</td>'
                  })
                  dIv += '<td>' + item.discount_price + '</td>';
                  // dIv += '<td>' + item.one_price + '</td>';
                  dIv += '<td>' + item.original_price + '</td>';
                  dIv += '<td>' + item.inventory + '</td>';
                  dIv += '</tr>'
                })
                dIv+='</tbody>'
                skuTbody.append(dIv);

                $(".total").on("click",function(){
                  var is_checked=$(this).prop('checked');
                  if(is_checked){
                    $(".checkbox").parents(".choose-gd").find("input[type='checkbox']").prop("checked",true);
                  }else{
                    $(".checkbox").parents(".choose-gd").find("input[type='checkbox']").prop("checked",false);
                  }
                });
                $('.child_ck').on('click',function(){
                  var flag1=true;
                  $("#t_b input").each(function(i){
                    if($("#t_b input").eq(i).prop("checked")==false){
                      flag1=false;
                    }
                    $('.total').prop('checked',flag1==true?true:false)
                  })
                })
              }
            });
          })

          $('.confirm_select_sku').click(function(){
            var is_checked = $(".attr_select .choose-gd tbody input[type='checkbox']:checked");
              var len = is_checked.length;
              if(len < 1){
                Utils.layer.msg('请选择属性');
                return;
              }
              $('.attr_select').hide()
              $('.attr_modification').show();
              var Th_tbody = document.getElementById("Th_tbody");
              $("#Th_tbody").empty();
              for (var i = 0; i < len; i++) {
                var attr_value = $("tbody input[type='checkbox']:checked").eq(i).attr("attr_value");
                var attr_img = $("tbody input[type='checkbox']:checked").eq(i).attr("attr_img");
                var original_price = $("tbody input[type='checkbox']:checked").eq(i).attr("original_price");
                var discount_price = $("tbody input[type='checkbox']:checked").eq(i).attr("discount_price");
                var inventory = $("tbody input[type='checkbox']:checked").eq(i).attr("inventory");
                var attribute_value_id = $("tbody input[type='checkbox']:checked").eq(i).attr("attribute_value_id");
                var attr_id = $("tbody input[type='checkbox']:checked").eq(i).attr("goods_attr_id");
                var id = $("tbody input[type='checkbox']:checked").eq(i).attr("id");
                var imgCdn = $("tbody input[type='checkbox']:checked").eq(i).attr("cdn");
                var str1 = attr_value ? '<td><input type="hidden" class="no_width deep layui-input" value="' + attr_value + '">' + attr_value + '</td>' : '<td style="display:none"><input type="hidden" class="no_width deep layui-input" value="' + attr_value + '" >' + attr_value + '</td>';
                if(!attr_value){
                  $('.attr_modification .choose-gd thead tr th.sku').hide()
                }else{
                  $('.attr_modification thead tr th.sku').show()
                }
                str1 += '<td><input type="hidden" class="no_width deep layui-input" value="' + attr_img + '"><img src="' + imgCdn + attr_img + '"/></td>'
                str1 += '<td><input type="text" class="no_width shallow layui-input" value="' + discount_price + '" placeholder="请输入单购价"></td>'
                // str1 += '<td><input type="text" class="no_width shallow layui-input" value="' + one_price + '"></td>'
                str1 += '<td><input type="text" class="no_width shallow layui-input" value="' + original_price + '"></td>'
                str1 += '<td><input type="text" class="no_width shallow layui-input" value="' + inventory + '"></td>'
                
                str1 += '<td style="display: none"><input type="hidden" value="' + attr_id + '"></td>'
                str1 += '<td style="display: none"><input type="hidden" value="' + attribute_value_id + '"></td>'
                str1 += '<td style="display: none"><input type="hidden" value="' + id + '"></td>'

                var Tr = document.createElement("tr");
                Tr.innerHTML = str1;
                Th_tbody.appendChild(Tr)
              }
          })

          $('.ok').click(function() {
            
            if($('.attr_select').css('display') != 'none'){
              Utils.layer.msg("请确认选择的SKU")
              return;
            }
              var id1 = $(".hide_id").val();
              var id2 = $(".ac_id").val();
              var len = $(".attr_modification .choose-gd tbody tr").length;
              var tArray = new Array();
              var at_name = $(".at_name").val();
              var b_time = $(".b-time").val();
              var e_time = $(".e-time").val();
              var join_num = $(".join_num").val();
              var goods_attr_id = $(".gd_id").val();
              var mk_type = $(".mk_type").val();
              var top = $('input[name="top"]:checked').val();
              for (var i = 0; i < len; i++) {
                  var gd_info = [];
                  for (var j = 0; j < 9; j++) {
                      gd_info.push($(".attr_modification .choose-gd tbody tr").eq(i).find("input").eq(j).val());
                  }
                  // one_price:gd_info[3],
                  tArray.push({ attr_img: gd_info[1], original_price: gd_info[2], collage_price: gd_info[3], inventory: gd_info[4], goods_attr_id: gd_info[5], activity_attr_id: gd_info[6], id: gd_info[7] });
              };
              if (at_name == "" || b_time == "" || e_time == "" || join_num == "") {
                  Utils.layer.msg("请完善信息");
              } else if (at_name.length > 45) {
                  Utils.layer.msg("名称不能超过45个字")
              } else if (join_num < 2 || join_num > 10) {
                  Utils.layer.msg("请输入2~10人")
              } else if (b_time >= e_time) {
                  Utils.layer.msg("结束的时间需大于开始时间")
              } else {
                // console.log({ active_attr: tArray, goods_id: id1, id: id2, active_name: at_name, active_num: join_num, begin_time: b_time, end_time: e_time, type: mk_type, set_top: top })
                  $.post("/active/update", { active_attr: tArray, goods_id: id1, id: id2, active_name: at_name, active_num: join_num, begin_time: b_time, end_time: e_time, type: mk_type, set_top: top },
                      function success(data) {
                          if (data.code == 200) {
                              Utils.layer.msg(data.msg);
                              setTimeout(function() {
                                  location.href = "/active/index";
                              }, 1000);
                          } else {
                              Utils.layer.msg(data.msg);
                          }
                      }
                  )
              }
          })

      })
  }